const querystring =  require('querystring')
const { 
  GraphQLSchema, 
  GraphQLObjectType,
  GraphQLString,
  GraphQLInt,
  GraphQLFloat,
  GraphQLList,
  GraphQLBoolean
} = require('graphql')

const axios = require('axios')

const PositionType = new GraphQLObjectType({
  name: 'PositionType',
  fields: {
    positionId: {
      type: GraphQLInt
    },
    positionName: {
      type: GraphQLString
    }
  }
})

const TheatersType = new GraphQLObjectType({
  name: 'TheatersType',
  fields: {
    id: {
      type: GraphQLInt
    },
    name: {
      type: GraphQLString
    }
  }
})

const MovieType = new GraphQLObjectType({
  name: 'MovieType',
  fields: {
    id: {
      type: GraphQLInt
    },
    title: {
      type: GraphQLString
    },
    genres: {
      type: GraphQLString
    },
    rating: {
      type: GraphQLFloat
    },
    theaters: {
      type: TheatersType,
      async resolve(obj) {
        let result = await axios.get('http://localhost:9000/theaters/' + obj.theater)
        return result.data
      }
    }
  }
})

const ResultType = new GraphQLObjectType({
  name: 'ResultType',
  fields: {
    ret: {
      type: GraphQLBoolean
    },
    data: {
      type: GraphQLString
    }
  }
})

const schema = new GraphQLSchema({
  query: new GraphQLObjectType({
    name: 'RootQueryType',
    fields: {
      hello: {
        type: GraphQLString,
        resolve() {
          return 'world'
        }
      },
      movie: {
        type: MovieType,
        async resolve() {
          let result = await axios.get('http://localhost:9000/subjects')
          return result.data[0]
        }
      },
      movies: {
        type: new GraphQLList(MovieType),
        async resolve() {
          let result = await axios.get('http://localhost:9000/subjects')
          return result.data
        }
      },
      position: {
        type: PositionType,
        async resolve() {
          let result = await axios.get('https://m.lagou.com/listmore.json')
          return result.data.content.data.page.result[0]
        }
      },

      positions: {
        type: new GraphQLList(PositionType),
        args: {
          pageNo: {
            type: GraphQLInt
          },
          pageSize: {
            type: GraphQLInt
          }
        },
        async resolve(obj, args) {
          let result = await axios({
            url: 'https://m.lagou.com/listmore.json',
            params: args
          })
          return result.data.content.data.page.result
        }
      }
    }
  }),

  mutation: new GraphQLObjectType({
    name: 'RouteMutationType',
    fields: {
      insert: {
        type: ResultType,
        args: {
          title: {
            type: GraphQLString
          },
          genres: {
            type: GraphQLString
          },
          rating: {
            type: GraphQLFloat
          },
          theater: {
            type: GraphQLInt
          }
        },
        async resolve(obj, args) {
          // axios({
          //   url: 'http://localhost:9000/subjects',
          //   method: 'post',
          //   data: args
          // })
          let result = await axios.post('http://localhost:9000/subjects', { ...args })
          if (result.data) {
            return {
              ret: true,
              data: 'ok'
            }
          } else {
            return {
              ret: false,
              data: 'fail'
            }
          }
        }
      },
      patch: {
        type: ResultType,
        args: {
          title: {
            type: GraphQLString
          },
          id: {
            type: GraphQLInt
          }
        },
        async resolve(obj, args) {
          let result = await axios.patch('http://localhost:9000/subjects/' + args.id, { ...args })
          if (result.data) {
            return {
              ret: true,
              data: 'ok'
            }
          } else {
            return {
              ret: false,
              data: 'fail'
            }
          }
        }
      },
      put: {
        type: ResultType,
        args: {
          title: {
            type: GraphQLString
          },
          id: {
            type: GraphQLInt
          }
        },
        async resolve(obj, args) {
          let result = await axios.put('http://localhost:9000/subjects/' + args.id, { ...args })
          if (result.data) {
            return {
              ret: true,
              data: 'ok'
            }
          } else {
            return {
              ret: false,
              data: 'fail'
            }
          }
        }
      },
      delete: {
        type: ResultType,
        args: {
          id: {
            type: GraphQLInt
          }
        },
        async resolve(obj, args) {
          let result = await axios.delete('http://localhost:9000/subjects/' + args.id)
          if (result.data) {
            return {
              ret: true,
              data: 'ok'
            }
          } else {
            return {
              ret: false,
              data: 'fail'
            }
          }
        }
      }
    }
  })
})

module.exports = schema